package main

import (
	"fmt"
	"log"
	"os"
	"os/user"

	"monkey/repl"
	"monkey/run"
)

func beginRepl() {
	currentUser, err := user.Current()
	if err != nil {
		panic(err)
	}

	fmt.Printf(
		"Hello %s! This is the Monkey programming language!\n",
		currentUser.Name,
	)
	fmt.Print("Fell free to type in commands\n")
	repl.Start(os.Stdin, os.Stdout)
}

func showUsageAndExit() {
	fmt.Println("usage: monkey [sourceFile]")
	os.Exit(0)
}

func main() {
	if len(os.Args) == 1 {
		beginRepl()
		os.Exit(0)
	}

	if len(os.Args) == 2 {
		if os.Args[1] == "-h" || os.Args[1] == "--help" {
			showUsageAndExit()
		}

		input, err := os.ReadFile(os.Args[1])
		if err != nil {
			log.Fatalf("Can't open file %q.", os.Args[1])
		}

		run.Start(string(input))
	}
}
